package com.lmonkey.servlet.product;

import com.lmonkey.dao.Basedao;
import com.lmonkey.shop.PRODUCT;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MultipartConfig
@WebServlet("/admin/admin_doproductupdate")
public class DoProductUpdate extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取参数
        String pid = request.getParameter("p_id");
        String pname = request.getParameter("productname");
        String id = request.getParameter("parentid");
        String desc = request.getParameter("productDesc");
        Float price = Float.parseFloat(request.getParameter("productprice"));
        int stock = Integer.parseInt(request.getParameter("productstock"));

        //获取part对象
        Part part = request.getPart("photo");
        //获取图片名称
        String filename = part.getSubmittedFileName();
        String fname = request.getParameter("file");
        //进行验证
        if (!filename.equals(fname)){
            //获取文件存储路径
            String path = "D:\\java\\obj\\web\\shangc\\image\\product";
            //将图片放入该目录(上传文件)
            part.write(path+"\\"+filename);
            //删除原来的图片文件
            String path1 = "D:\\java\\obj\\web\\shangc\\image\\product\\"+fname+"";
            File cfile = new File(path1);
            try {
                cfile.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        //连接数据库
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;

        //创建实体
        PRODUCT p = new PRODUCT(Integer.parseInt(pid),pname,desc,price,stock,Integer.parseInt(id.split("-")[0]),Integer.parseInt(id.split("-")[1]),filename);
        int count = 0;
        try {
            String sql = "update lmonkey_product set product_name = ? ,product_description = ?, product_price = ?, product_stock = ?, product_fid = ?, product_cid = ?,product_filename = ? where product_id =?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1,p.getProduct_name());
            ps.setObject(2,p.getProduct_description());
            ps.setFloat(3,p.getProduct_price());
            ps.setInt(4,p.getProduct_stock());
            ps.setInt(5,p.getProduct_fid());
            ps.setInt(6,p.getProduct_cid());
            ps.setObject(7,p.getProduct_filename());
            ps.setInt(8,p.getProduct_id());
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (count>0){
            response.sendRedirect("admin_productselect?cp="+request.getParameter("cpage")+"&keywords="+request.getParameter("keywords"));
        }else{
            PrintWriter out = response.getWriter();
            out.println("<script>alert('修改失败');location.href='admin_toproductupdate?cp="+request.getParameter("cpage")+"&keywords="+request.getParameter("keywords")+"&id="+pid+"'</script>");
            out.close();
        }
    }
}
